<?php
namespace App\Controller;
use Cake\Cache\Cache;

class CommentReplysController extends AppController{

    public $displayNum = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：设置指定评论回复状态
   * */
    public function setCommentReply()
    {
        $id = $this->request->getData("id");
        $enabled = $this->request->getData("enabled");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if($enabled !=0 && $enabled !=1){
            $this->G->error("enabled_error");
            return null;
        }
        $comment_replys = $this->CommentReplys->findById($id)->first();
        $this->CommentReplys->query()
            ->update()
            ->set(['enabled'=>$enabled])
            ->where(['id' => $id])
            ->execute();
        Cache::delete('list_'.$comment_replys['comment_content_id'], 'comment_replys');
        Cache::delete('content_' . $comment_replys['id'], 'comment_replys');
        Cache::delete('total_'.$comment_replys['comment_content_id'], 'comment_replys');
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取指定评论内容下的回复
   * */
    public function getCommentReplies()
    {
        $comment_content_id = $this->request->getData("comment_content_id");
        $nickname = $this->request->getData("nickname");
        $page = $this->request->getData("page");
        $page = empty($page) ? 1 : $page;
        $where = [];
        if(!empty($comment_content_id)){
            $where['comment_content_id'] = $comment_content_id;
        }
        if(!empty($nickname)){
            $this->loadModel('UserInfos');
            $user_infos = $this->UserInfos->find()
                ->select(['user_id'])
                ->where(['nickname like '=>'%'.$nickname.'%'])
                ->toArray();
            $where['user_id in'] = empty($user_infos) ? [0] : array_column($user_infos, 'user_id');
        }
        $comment_replys = $this->CommentReplys->find()
            ->where($where)
            ->order(['id desc'])
            ->limit($this->displayNum)
            ->page($page);
        $re = [];
        $count = $comment_replys->count();
        $re['pageTotal'] = ceil($count/$this->displayNum);
        $comment_replys = $comment_replys->toArray();
        $re['list'] = [];
        if(!empty($comment_replys)){
            $this->loadComponent('UserCache');
            $this->loadComponent('ContentFormat');
            foreach ($comment_replys as $k=>$v){
                $user = $this->UserCache->getVal($v['user_id'], null, 'UserInfos');
                $to_user = $this->UserCache->getVal($v['to_user'], null, 'UserInfos');
                $re['list'][] = [
                    'id'=>$v['id'],
                    'comment_content_id'=>$v['comment_content_id'],
                    'user_id'=>$v['user_id'],
                    'nickname'=>$user['nickname'],
                    'to_user'=>$v['to_user'],
                    'to_user_nickname'=>empty($to_user) ? '' : $to_user['nickname'],
                    'content'=>$this->ContentFormat->add($v['content']),
                    'enabled'=>$v['enabled'],
                    'add_time'=>date('Y-m-d H:i:s', $v['add_time'])
                ];
            }
        }
        $this->G->success('Success', $re);
    }

}
?>